import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';

import '../style/app_colors.dart';

class HeaderLockList extends StatefulWidget {
  const HeaderLockList({super.key});

  @override
  State<HeaderLockList> createState() => _HeaderLockListState();
}

class _HeaderLockListState extends State<HeaderLockList> {
  // Header是否可见
  bool _isHeaderVisible = false;
  // Header高度
  final double _headerHeight = 200.0.h;

  // 滚动控制器
  final ScrollController _scrollController = ScrollController();

  @override
  void initState() {
    super.initState();

    // 监听滚动事件
    _scrollController.addListener(_onScroll);
  }

  @override
  void dispose() {
    _scrollController.removeListener(_onScroll);
    _scrollController.dispose();
    super.dispose();
  }



  // 处理滚动事件
  void _onScroll() {
    if (_scrollController.position.userScrollDirection ==
        ScrollDirection.forward) {
      setState(() {
        WidgetsBinding.instance.addPostFrameCallback((_) {
          _isHeaderVisible = true;
        });


      });
    }
    // 当用户向下滚动时隐藏header
    else if (_scrollController.position.userScrollDirection ==
        ScrollDirection.reverse) {
      setState(() {
        WidgetsBinding.instance.addPostFrameCallback((_) {
          _isHeaderVisible = false;
        });

      });
    }

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Header隐藏后锁定滚动'),
      ),
      body: ListView(
        controller: _scrollController,
        physics: const BouncingScrollPhysics(
          parent: AlwaysScrollableScrollPhysics(),
        ),
        children: [
          // 动态Header
          AnimatedContainer(
            duration: const Duration(milliseconds: 500),
            curve: Curves.easeInOut,
            height: _isHeaderVisible ? _headerHeight : 0,

            padding: EdgeInsets.all(20),
            child: Container(
              alignment: Alignment.center,
              child: ListView(
                children: [
                  Container(
                    width: MediaQuery.of(context).size.width,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(20.h)),
                        color: Colors.white
                    ),
                    child: Row(
                      children: [
                        Image.asset('assets/images/icon_player_audio_waver.png',width: 300.w, height: 48.h),
                      ],
                    ),
                  ),
                  SizedBox(height: 5.h,),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('00:37:00',style: TextStyle(
                          fontSize: 12.sp,
                          color: AppColors.second_title_color
                      ),),
                      Text('01:00:00',style: TextStyle(
                          fontSize: 12.sp,
                          color: AppColors.second_title_color
                      ))
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Container(
                        child: Column(
                          children: [
                            Image.asset('assets/images/icon_transcript_audio_play_speeds.png',width: 24.w,height: 24.h,),
                            SizedBox(height: 10.h,),
                            Text('播放速度',
                              style: TextStyle(
                                  fontSize: 12.sp,
                                  color: AppColors.second_title_color
                              ),
                            )
                          ],
                        ),
                      ),
                      Container(
                        child: Column(
                          children: [
                            Image.asset('assets/images/icon_transcript_audio_back_seconds.png',width: 24.w,height: 24.h,),
                            SizedBox(height: 10.h,),
                            Text('后退10秒',
                              style: TextStyle(
                                  fontSize: 12.sp,
                                  color: AppColors.second_title_color
                              ),
                            )
                          ],
                        ),
                      ),
                      Container(
                        child: Column(
                          children: [
                            Image.asset('assets/images/icon_play_realtime_btn.png',width: 48.w,height: 48.h),
                            SizedBox(height: 10.h,),
                            Text('00:05',
                              style: TextStyle(
                                  fontSize: 12.sp,
                                  color: AppColors.second_title_color
                              ),
                            )
                          ],
                        ),
                      ),
                      Container(
                        child: Column(
                          children: [
                            Image.asset('assets/images/ico_transcript_audio_forward_seconds.png',width: 24.w,height: 24.h,),
                            SizedBox(height: 10.h,),
                            Text('快进10秒',
                              style: TextStyle(
                                  fontSize: 12.sp,
                                  color: AppColors.second_title_color
                              ),
                            )
                          ],
                        ),
                      ),
                      Container(
                        child: Column(
                          children: [
                            Text('',
                              style: TextStyle(
                                  fontSize: 12.sp,
                                  color: AppColors.second_title_color
                              ),
                            )
                          ],
                        ),
                      )
                    ],
                  )
                ],
              ),
            ),
          ),
          ListTile(
            leading: const Icon(Icons.lock),
            title: Text('列表项 1'),
            subtitle: const Text('Header隐藏后无法继续上推'),
          ),
          ListTile(
            leading: const Icon(Icons.lock),
            title: Text('列表项 2'),
            subtitle: const Text('Header隐藏后无法继续上推'),
          )
        ],
      ),
    );
  }
}